/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo.DAL;
import Modelo.RutaEscape;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import org.json.JSONObject;
import org.json.JSONArray;

/**
 *
 * @author Patricia
 */
public class RutaEscape_DAO {

    public static boolean insertarRutaEscape(RutaEscape rutaEscape){
        ConexionBD conexion = new ConexionBD();
        Statement s = null;

        try {
          s = conexion.session.createStatement();
        } catch (SQLException se) {
          System.out.println("Error Insertando Ruta de Escape!");
          return false;
        }
        String query = "INSERT INTO ruta_escape(id_piso, nombre_sede, nombre_edificio,"
                + " fecha_revision, usada, ruta_foto) VALUES"
                + " ("+rutaEscape.getPiso()+","
                + " '"+rutaEscape.getSede()+"',"
                + " '"+rutaEscape.getNombreEdificio()+"',"
                + " to_date('"+rutaEscape.getFechaRevision()+"', 'dd/mm/yyyy'), "
                + (rutaEscape.getUsada()== null ? "'no'," :"'"+rutaEscape.getUsada()+"',")
                + " '"+rutaEscape.getRutaFoto()+"');";
        System.out.println(query);
        try {
            s.executeUpdate(query);
            return true;
        } catch (SQLException se) {
          System.out.println("Error Insertando Ruta de Escape! "+se.getMessage());
          return false;
        }
    }

    public static JSONArray consultarRutasEscape(String query) {
        ConexionBD conexion = new ConexionBD();
        Statement s = null;
        ResultSet ruta = null;
        try {
            s = conexion.session.createStatement();
        } catch (SQLException se) {
            System.out.println("Error Consultando Rutas de Escape!");
            return null;
        }
        System.out.println(query);
        try {
            ruta = s.executeQuery(query);

            JSONArray rutas = new JSONArray();
            try {
                int i = 0;
                while (ruta.next()) {
                    JSONObject rut = new JSONObject();
                    rut.put("sede",ruta.getString(1));
                    rut.put("edificio", ruta.getString(2));
                    rut.put("piso", ruta.getString(3));
                    rut.put("fecha", ruta.getString(4));
                    rut.put("usada", ruta.getString(5));
                    rut.put("ruta", ruta.getString(6));
                    rut.put("tam", ruta.getString(7));
                    rutas.put(i++, rut);
                }
            } catch (Exception e) {
            }
            //System.out.println("\n\n\n^RiesgosDAO: "+riesgos);
            return rutas;
        } catch (SQLException se) {
            se.printStackTrace();
            System.out.println("Error Consultado Rutas de Escape");
            return null;
        }
    }
}
